<template>
  <div class="exam-pagercontainer">
    <el-container style="height:calc(100vh - 40px );background-color: #fff;">
      <el-aside width="80px" class="exam-pager-aside">
        <FunctionList />
      </el-aside> 

      <el-main class="exam-pager-main">
        <el-scrollbar style="">
          <div class="tpl-app" >

            <div style="width: calc(100% - 0px);margin-top: 10px;" v-loading="sceneLoading">

              <div class="search-container-panel">
                <el-row>
                  <el-col :span="24">
                    <div class="feature-team-box">
                      <div style="gap: 12px;">
                        <h1
                          style="font-size: 20px; font-weight: 500; font-style: normal; line-height: 32px; color: rgba(var(--coze-fg-4), var(--coze-fg-4-alpha)); margin: 0px 0px 0px 10px; float: left;">
                          试卷列表 
                        </h1>
                      </div>
                      <div class="search-container-weDuEn">
                        <el-input 
                          v-model="searchQuery" 
                          style="width: 400px" 
                          size="large" 
                          placeholder="搜索试卷名称" 
                          :suffix-icon="Search"
                          @input="handleSearch"
                          clearable
                        />
                      </div>
                    </div>
                  </el-col>
                </el-row>
              </div>

              <div class="channel-container-panel" style="margin-top:20px">
                <el-row>
                  <el-col :span="6" v-for="(item, index) in filteredPagerList" :key="index" style="padding:8px;">
                    <div class="exam-pager-card-container" @click="enterExamPager(item)">
                      <article class="exam-pager-card">
                        <div class="exam-pager-card-content">
                          <div class="scene-header">
                            <span class="scene-title"> <i class="fa-solid fa-file-word"></i> {{ item.title }}</span>
                          </div>
                          <div class="scene-author-info">
                            <span class="scene-name">
                              <i class="fa-solid fa-user-shield"></i>
                              难度
                            </span>
                            <span class="scene-name">
                              {{ item.difficulty === 'easy' ? '简单': item.difficulty === 'medium' ? '中等': item.difficulty === 'hard' ? '困难' :'未确认' }}
                            </span>
                          </div>
                          <div class="scene-description">
                            {{ item.description }}
                            填空题 6 题
                          </div>
                          <div class="semi-divider semi-divider-horizontal"></div>
                          <div class="scene-footer">
                            <div class="scene-price">
                              <el-tag v-if="item.sceneScope == 'private'" type="info"><i class="fa-solid fa-lock" />
                                考试中
                              </el-tag>
                              <el-tag v-else-if="item.sceneScope == 'public'" type="info"><i
                                  class="fa-solid fa-globe" /> 公开</el-tag>
                              <el-tag v-else type="info"><i class="fa-solid fa-truck-plane" />
                                考试中
                              </el-tag>
                            </div>
                            <div class="scene-tag">
                              <div class="scene-stats">
                                <span>添加</span>
                                <span>{{ item.addTime}}</span>
                              </div>
                            </div>
                          </div>
                        </div>
                      </article>
                    </div>
                  </el-col>

                  <el-col :span="24" v-if="filteredPagerList.length == 0">
                    <el-empty :image-size="400" :image="learnLogo" description="没有找到匹配的试卷" />
                  </el-col>

                </el-row>
              </div>
            </div>

          </div>
        </el-scrollbar>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import FunctionList from './functionList'
import { pagerListByPage } from '@/api/base/im/scene/examPaper';
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import learnLogo from '@/assets/icons/tech_01.svg';
import { Search } from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();

const sceneId = ref(route.query.sceneId);
const sceneLoading = ref(true);
const pagerList = ref([]);
const searchQuery = ref('');

// 计算属性：过滤后的试卷列表
const filteredPagerList = computed(() => {
  if (!searchQuery.value) {
    return pagerList.value;
  }
  return pagerList.value.filter(item => 
    item.title.toLowerCase().includes(searchQuery.value.toLowerCase())
  );
});

// 搜索处理函数
const handleSearch = () => {
  // 计算属性会自动更新，这里不需要额外处理
};

/** 进入长文本编辑界面 */
function enterExamPager(item) {
  const path = '/scene/examPager/examPagerView';
  router.push({
    path: path,
    query: { 
      'pagerId': item.id,
      'sceneId': sceneId.value
    }
  });
}

/** 获取场景列表 */
function handlePagerListByPage() {
  pagerListByPage(sceneId.value).then(res => {
    pagerList.value = res.data;
    sceneLoading.value = false;
  }).catch(err => {
    sceneLoading.value = false;
  });  
}

onMounted(() => {
  handlePagerListByPage();
});
</script>

<style lang="scss" scoped>
/* 保持原有的样式不变 */
.exam-pager-card-container {
  display: flex;
  flex-grow: 1;
  border-radius: 8px;


}

  .tpl-app{
    display: flex;
    margin-left: 0px;
    width:100%;
    background-color: #fff;
    height:calc(100vh - 40px)
  }

.exam-pager-card {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  overflow: hidden;
  border: 1px solid rgba(6, 7, 9, 0.1);
  border-radius: 8px;
  background-color: #fff;
  cursor: pointer;
  gap: 10px;
  padding: 15px;
  padding-bottom:20px;
  transition: box-shadow 0.3s;

  &:hover {
    box-shadow: var(--coz-shadow-large);
  }

  .exam-pager-image-container {
    position: relative;
    width: 100%;
    height: 10px;
    overflow: hidden;

    .exam-pager-card-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
      object-position: center;
    }
  }

  .exam-pager-card-content {
    margin-top: 10px;
    padding: 0 4px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    width: calc(100%);
    padding-left: 10px;

    .scene-header {
      display: flex;
      align-items: flex-start;
      gap: 8px; 
      margin: 8px 0px;
      margin-top: 0px;
      padding-left: 0px;
      flex-direction: column;

      .scene-title {
        font-weight: bold;
        font-size: 16px;
        line-height: 22px;
        color: var(--coz-fg-primary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .scene-tag {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 2px 4px;
        border-radius: 4px;
        background-color: var(--coz-tag-primary);
        color: var(--coz-fg-primary);
        font-weight: 500;
        font-size: 12px;
        line-height: 16px;
        min-height: 20px;
        white-space: nowrap;
        user-select: none;

        svg {
          width: 1em;
          height: 1em;
          fill: currentColor;
        }

        span {
          margin-left: 2px;
        }
      }
    }

    .scene-author-info {
      display: flex;
      align-items: center;
      gap: 4px;
      margin-top: 4px;

      .scene-avatar {
        width: 14px;
        height: 14px;
        border-radius: 50%;
      }

      .scene-name,
      .scene-username {
        font-weight: 500;
        font-size: 12px;
        line-height: 16px;
        color: var(--coz-fg-primary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    .scene-description {
      margin-top: 8px;
      margin-bottom: 8px;
      font-size: 14px;
      line-height: 20px;
      color: var(--coz-fg-secondary);
      display: -webkit-box;
      -webkit-box-orient: vertical;
      height: 40px;
      overflow: hidden;
      display: -webkit-box;
      display: -moz-box;
      display: box;
      -webkit-box-orient: vertical;
      -moz-box-orient: vertical;
      box-orient: vertical;
      -webkit-line-clamp: 2;
      -moz-line-clamp: 2;
      line-clamp: 2;
    }

    .scene-footer {
      margin-top: 4px;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .scene-price {
        font-weight: 500;
        font-size: 14px;
        line-height: 22px;
        color: var(--coz-fg-primary);
      }

      .scene-stats {
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: 12px;
        line-height: 16px;
        color: var(--coz-fg-secondary);
      }
    }
  }
}
</style>